package com.zrz.util;

import com.squareup.okhttp.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;

public class HttpUtils {
    public static String okGet(String url) {
        LogUtils.debug("url--->" + url);
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).get().build();
        String result = null;
        try {
            Response response = client.newCall(request).execute();
            result = response.body().string();
        } catch (IOException e) {
            LogUtils.debug("ok get 异常:" + e.getLocalizedMessage());
            e.printStackTrace();
        }
        LogUtils.debug("result" + result);
        return result;
    }

    public static String okPost(String url, String bodyContent) {
        LogUtils.info("url--->" + url);
        LogUtils.info("bodyContent" + bodyContent);
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(JSON, bodyContent);
        Request request = new Request.Builder().url(url).post(body).build();
        try {
            Response response = client.newCall(request).execute();
            String result = response.body().string();
            LogUtils.info("result" + result);
            return result;
        } catch (Exception ex) {
            LogUtils.debug("异常:" + ex.getLocalizedMessage());
            return null;
        }
    }

    public static String okGetWithHeader(String url) {
        try {
            LogUtils.debug("url--->" + url);
            OkHttpClient client = new OkHttpClient();
            HttpServletRequest servletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String currUrl = url+"?"+parameters(servletRequest);
            Request request = new Request.Builder().url(currUrl).addHeader("Head",servletRequest.getHeader("Head")).get().build();
            String result = null;
            try {
                Response response = client.newCall(request).execute();
                result = response.body().string();
            } catch (IOException e) {
                LogUtils.debug("ok get 异常:" + e.getLocalizedMessage());
                e.printStackTrace();
            }
            LogUtils.debug("result" + result);
            return result;
        } catch (Exception ex) {
            LogUtils.debug("异常:" + ex.getLocalizedMessage());
            return null;
        }
    }

    private static String parameters(HttpServletRequest request) {
        StringBuffer obj = new StringBuffer();
        Enumeration<?> enu = request.getParameterNames();
        while (enu.hasMoreElements()) {
            String paraName = (String) enu.nextElement();
            obj.append(paraName);
            obj.append("=");
            obj.append(request.getParameter(paraName));
            obj.append("&");
        }
        obj =  obj.deleteCharAt(obj.length() - 1);
        return obj.toString();
    }

}
